﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SusuCMS.Web.Paging;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace SusuCMS
{
    public class Page
    {
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        [AllowHtml]
        public string Content { get; set; }

        [AllowHtml]
        public string ExtensionValue { get; set; }

        public DateTime CreateTime { get; set; }

        public virtual Website Website { get; set; }

        public int WebsiteId { get; set; }

        public DateTime PublishTime { get; set; }

        [Required]
        [RegularExpression("^([a-zA-Z0-9_\\+-]+/{0,1})*$", ErrorMessage = "Not a valid url")]
        public string Url { get; set; }

        public string ImagePath { get; set; }

        public virtual PageType PageType { get; set; }

        public int PageTypeId { get; set; }

        public bool IsPublished { get; set; }

        public virtual ICollection<PageImage> PageImages { get; internal set; }

        public Dictionary<string, string> GetExtensionDictionary()
        {
            var dictionary = new Dictionary<string, string>();
            var list = new JavaScriptSerializer().Deserialize<List<KeyValue>>(this.ExtensionValue);
            foreach (var item in list)
            {
                dictionary.Add(item.Name, item.Value);
            }

            return dictionary;
        }

        class KeyValue
        {
            public string Name { get; set; }

            public string Value { get; set; }
        }
    }
}
